<template>
	<layout :loading="loading" curPage="company" title="企业">
		
		<block slot="title">
			<auth url="/admin/company/export" class="layui-btn layui-btn-sm">
				<view @tap="downloadExcel">
					批量导出
				</view>
			</auth>
			
			<auth url="/admin/company/imports" class="layui-btn layui-btn-sm">
				<view @tap="uploadExcel">
					批量导入
				</view>
			</auth>
			<authNav href="/pages/company/companyEdit" url="/admin/company/save" class="layui-btn layui-btn-sm">
				<i class="layui-icon layui-icon-add-1"></i>
				添加
			</authNav>
		</block>
		
		<conditions :list="conditonList" :conditions="conditions" @submit="submitSearch"></conditions>

		<div class="layui-form layui-border-box layui-table-view">

			<tableList :list="page.list" :canSelect="false">

				<block slot="head">
					<th>企业名称</th>
					<th class="autoWidth">企业信息</th>
					<th>联系人</th>
					<th>运维公司</th>
					<th>操作</th>
				</block>

				<block slot="body">

					<tr v-for="( x , index) in page.list" :key="index">
						<td>
							<view class="mainText">
								{{x.name}}
							</view>
							<view class="text-muted fz12">
								行业：{{x.industry}}
							</view>
							<view class="text-muted fz12">
								{{x.createTime}}
							</view>
						</td>
						<td>
							<view @tap="chooseLocation(x)">
								<text class="layui-icon-release layui-icon mainText fz12" style="margin-right: 10px;cursor: pointer;"></text>
								<view style="display: inline-block;">
									<moreTip>
										{{x.address}}
									</moreTip>
								</view>
							</view>
							<view class="text-muted fz12">
								通用名:{{x.simpleName}}
							</view>
							<view class="text-muted fz12">
								曾用名:{{x.nameUsedBefore}}
							</view>
						</td>
						<td>
							{{x.contacts}}
							<view class="text-muted fz12">
								<copy :text="x.mobile">
									{{x.mobile}}
								</copy>
							</view>
						</td>
						<td>
							<view v-for="( item , curIndex) in x.list" :key="curIndex">
								{{item.name}}
							</view>
							<view v-if="x.list.length == 0">
								暂无运维公司
							</view>
						</td>
						<td>
							<authNav :href="'/pages/station/stationList?ts.companyId=' + x.id" url="/admin/station/list" class="layui-btn layui-btn-xs">
								点位
							</authNav>
							<authNav :href="'/pages/company/companyEdit?id=' + x.id" url="/admin/company/save" class="layui-btn layui-btn-xs">
								编辑
							</authNav>
							<authBtn :url="'/admin/company/delete?id='+x.id" confirm="delete" class="layui-btn layui-btn-xs">
								删除
							</authBtn>
						</td>
					</tr>

				</block>

			</tableList>
			
			<emptyTips :loading="loading" :list="page.list"></emptyTips>

			<paginate :page="page" @switchPage="switchPage" @changePageSize="changePageSize"></paginate>
			
		</div>


	</layout>
</template>

<script>
	export default {
		data() {
			return {
				conditonList:[
					{title:"企业名称",name:"tc.name",type:"varchar"},
					{title:"企业地址",name:"tc.address",type:"varchar"},
					{title:"手机号码",name:"tc.mobile",type:"mobile"}
				],
				conditions: {},
				loading: true ,
				action: "/admin/company/list",
				page: {
					pageNumber: 1,
					lastPage: true,
					totalPage: 1,
					list: [],
					totalRow: 0,
					pageSize: 10
				}
			}
		},
		
		onLoad:function(res){
			this.conditions = res || {} ;
		},

		onShow() {
			this.kit.getPage(this.action,function(res){
			});
		},

		methods: {
			submitSearch:function(conditions){
				this.conditions = conditions ;
				this.switchPage(1);
			},
			
			switchPage: function(pageNumber) {
				this.page.pageNumber = pageNumber || this.page.pageNumber ;
				this.kit.getPage(this.action);
			},
			
			changePageSize: function(pageSize) {
				this.page.pageSize = pageSize;
				this.page.pageNumber = 1;
				this.kit.getPage(this.action);
			},
			
			uploadExcel:function(res){
				uni.showModal({
					title:'提示',
					content:'使用批量导入功能时，如系统已录入该企业则会根据excel更新企业数据。请按照导入模板格式制作您的excel文件，仅支持.xls格式的excel文件。',
					showCancel:true,
					confirmColor:'#5d83f7' ,
					confirmText:"开始导入" ,
					cancelText: "下载模板" ,
					success : (e)=>{
						if(e.confirm){
							uni.chooseFile({
								fileType : "application/vnd.ms-excel" ,
								success :(res) =>{
									var path = res.path ;
									this.upload(path);
								}
							});
						}else{
							this.kit.download("/admin/company/getUploadTpl?tplName=importCompany.xls&reName=企业信息导入模板.xls");
						}
					}
				});
			},
			
			upload : function(path){
				this.kit.uploadFile("/admin/company/imports" , path ,(e)=>{
					this.page.pageNumber = 1 ;
					this.kit.getPage(this.action);
					uni.showModal({
						title:'提示',
						content:`本次共导入${e.saveCount}条数据，更新${e.updateCount}条数据，未导入${e.skipCount}条数据。`,
						showCancel:false,
						confirmColor:'#3385ff',
						success : (e)=>{
							
						}
					});
				});
			},
			
			downloadExcel:function(res){
				var data = this.conditions;
				uni.showLoading({
					title:"请稍后...",
					mask: true 
				});
				this.kit.get("/admin/company/export",data, (res) => {
					uni.hideLoading();
					this.kit.download("/admin/company/downloadExcel?fileName=" + res.fileName );
				});
			},
			
			chooseLocation:function(e){
				console.log(e);
				uni.openLocation({
					latitude:e.latitude,
					longitude:e.longitude,
					name : e.name ,
					address:e.address + e.addressName
				})
			},
		}
	}
</script>